/*******************************************************************************
 * Copyright 2012 Francesco Foresti
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
/**
 * 
 */
package it.fflabs.testService;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.log4j.Logger;

/**
 * <b>gwt-dynScript-testService : MyTestService</b><br>
 * <hr>
 * Comment: This is a REST service root resource.<br>
 * <hr>
 * 18/dic/2011<br>
 * 
 * @author Francesco Foresti
 * 
 */
@Path("")
public class MyTestService {

	public class TestObject {
		public String value = "Hello from your rest test service";
		public String anotherValue = ". What's Your name?";

// 		try these two instead :
//		public int value = 7;
//		public int anotherValue = 3;
		
//		then, see the different result with this couple		
//		public String value = "7";
//		public String anotherValue = "3";
	}
	
	Logger log = Logger.getLogger(MyTestService.class);
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response getGreeting(@QueryParam("callback") String callback) {

		// the response content must be 
		// - serialized in JSON
		// - wrapped with the callback name and
		//   parenthesys, just like a method invocation
		
		String serializedContent = JSONUtils.serialize(new TestObject());
		log.info("returning " + serializedContent);
		return Response.ok((callback == null ? serializedContent : callback + "(" + serializedContent + ");"), MediaType.APPLICATION_JSON).build();
	}
}
